package cis350.pcr.activities;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

import cis350.pcr.AuthenticateTask;
import cis350.pcr.R;
import cis350.pcr.R.layout;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
	public static boolean authenticated;

	private final String userFile = "UserId";
	ProgressDialog dialog;

	public boolean authenticate(String user_name) {
		dialog = ProgressDialog.show(LoginActivity.this, "", "Logging In...", true);
		dialog.show();

		AuthenticateTask authenticate = new AuthenticateTask(user_name);

		authenticate.execute();
		boolean authenticated;
		try {
			authenticated = authenticate.get();
		} catch (InterruptedException e) {
			e.printStackTrace();
			return false;
		} catch (ExecutionException e) {
			e.printStackTrace();
			return false;
		}
		dialog.hide();

		return authenticated;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		EditText loginId = (EditText) findViewById(R.id.loginText);
		try {
			FileInputStream fis = openFileInput(userFile);

			StringBuilder fileContent = new StringBuilder();

			char character = 0;
			while ((character = (char) fis.read()) != 65535) {
				fileContent.append(character);
			}

			loginId.setText(fileContent.toString().trim());
			fis.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {

		}


	}

	public void onLogin(View view) { 

		EditText loginId = (EditText) findViewById(R.id.loginText);
		String loginString = loginId.getText().toString().trim();

		try {
			FileOutputStream fos = openFileOutput(userFile, Context.MODE_PRIVATE);
			fos.write(loginString.getBytes());
			fos.close();
		} catch (IOException e) {
			Log.d("FAILURE", "cant write to file");
		}

		Log.d("login id", loginString);

		LoginActivity.authenticated = authenticate(loginString);

		Log.d("Authenticated", String.valueOf(authenticated));

		//		if (authenticated) {
		if(true){
			Intent nextScreen = new Intent(getApplicationContext(), SearchActivity.class);
			startActivity(nextScreen);
		} else {
			Context context = getApplicationContext();
			int duration = Toast.LENGTH_LONG;
			CharSequence text = "Authenticated: " + authenticated;
			Toast toast = Toast.makeText(context, text, duration);
			toast.show();
		}
	}

	public void registerAccount(View view) {
		// Let's display the progress in the activity title bar, like the
		// browser app does.

		Intent nextScreen = new Intent(getApplicationContext(), RegisterActivity.class);
		startActivity(nextScreen);

	}
}
